<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>分类版主添加</title>
  <link rel="stylesheet" href="__LAYUI__/css/layui.css">
  <style>
  </style>
</head>

<body>
  <div class="form-container">
    <form class="layui-form" lay-filter="adminForm">
      <!-- 用户ID选择框 -->
      <div class="layui-form-item">
        <label class="layui-form-label">用户ID</label>
        <div class="layui-input-block">
          <select name="user_id" lay-verify="required" lay-search>
            <option value="">-- 请选择用户 --</option>
            {foreach $user as $v}
            <option value="{$v.id}">ID:{$v.id} - QQ:{$v.qq} - 昵称:{$v.nickname}</option>
            {/foreach}
          </select>
        </div>
      </div>

      <!-- 分类名称选择框 -->
      <div class="layui-form-item">
        <label class="layui-form-label">分类名称</label>
        <div class="layui-input-block">
          <select name="cate_id" lay-verify="required">
            <option value="">-- 请选择分类 --</option>
            {foreach $cate as $v}
            <option value="{$v.id}">{$v.cate_name}</option>
            {/foreach}
          </select>
        </div>
      </div>

      <!-- 是否启动单选框 -->
      <div class="layui-form-item">
        <label class="layui-form-label">是否启动</label>
        <div class="layui-input-block">
          <input type="radio" name="status" value="1" title="启用" checked>
          <input type="radio" name="status" value="0" title="禁用">
        </div>
      </div>

      <!-- 表单提交按钮 -->
      <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn" lay-submit lay-filter="submitForm">提交设置</button>
          <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
      </div>
    </form>
  </div>
  <script src="__LAYUI__/layui.js"></script>
  <script>
    layui.use(['form', 'layer'], function () {
      let form = layui.form;
      let layer = layui.layer;
      let $ = layui.$

      // 表单渲染（确保选择框和单选框显示正确）
      form.render('select');
      form.render('radio');

      // 表单提交处理
      form.on('submit(submitForm)', function (data) {
        $.ajax({
          url: '/admin/categoryAdmin/doAdd',
          type: 'POST',
          data: data.field,
          success: function (res) {
            layer.closeAll('loading');
            if (res.code == 200) {
              layer.msg('设置成功！');
              // 重置表单
              $('form[lay-filter="adminForm"]')[0].reset();
              form.render();
            } else {
              layer.msg(res.msg, { icon: 2 });
            }
          },
          error: function () {
            layer.closeAll('loading');
            layer.msg('服务器错误', { icon: 2 });
          }
        });

        // 阻止表单默认提交
        return false;
      });
    });
  </script>
</body>

</html>